package com.scalar.chiptrack.operations.inventoryps.actioncommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.inventoryps.forms.InventoryPackingSlipForm;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;

public class InventoryPackingSlipLoadLotNumbersCommand extends ActionCommand 
{
	private static final String ACTION_NAME = "/inventoryps.do" ;
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
    	InventoryPackingSlipForm inventoryPSForm = (InventoryPackingSlipForm) form;

        HashMap<String, String> errors = new HashMap<String, String>();
        HashMap<String, String> info = new HashMap<String, String>();
        String action = request.getParameter("action");

        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }
        
        try
        {

            inventoryPSForm.setBaseprocessType( NewProductDAO.getBaseProcessName( null, inventoryPSForm.getProcessType() ) );

            ArrayList<LotDetails> availableLotList = null;
            if (inventoryPSForm.getProcessType().equalsIgnoreCase( LOCATION_TYPE_DIE_BANK ) )
            {
                int locationId = 0;
                if (inventoryPSForm.getLocationId() > 0 )
                {
                    locationId =VendorDAO.getLocationIdByLTypeandVendorId( inventoryPSForm.getLocationId(), LOCATION_TYPE_DIE_BANK );
                }
                availableLotList = DataEntryDAO.loadAllDieBankWipLots(inventoryPSForm.getProcessStepId(), locationId, inventoryPSForm.getLotId(), false, false  );
            }
            else
            {
                availableLotList = DataEntryDAO.getAllRunningLotsByProcess( inventoryPSForm.getProcessType(), inventoryPSForm.getProcessStepId(), inventoryPSForm.getLocationId(), inventoryPSForm.getLotId() );
            }
            
            if ( availableLotList == null || availableLotList.size() == 0 )
            {
                info.put("No " + inventoryPSForm.getProcessType() + " WIP lots available" , null );
            }
        	request.setAttribute( "lotsList", availableLotList );
        	request.setAttribute( "processTypeList", NewProductDAO.loadAllProcessesFromDB( null, false ) );
        	request.setAttribute( "partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( inventoryPSForm.getProcessType() ) );
        	request.setAttribute( "locationList",    VendorDAO.getVendorNamesByPsType( null, inventoryPSForm.getProcessType()) );
        	request.setAttribute( "toLocationList",    VendorDAO.getAllVendors() );

            if ( inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_FAB ) || inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
                    || inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_SORT ) || inventoryPSForm.getBaseprocessType().equalsIgnoreCase( LOCATION_TYPE_DIE_BANK )
                    || "Back Grind".equalsIgnoreCase(inventoryPSForm.getProcessType() ) )
        	{
        		request.setAttribute( "showWaferIds", "TRUE" );
        	}
            else
            {
                request.setAttribute( "showWaferIds", "FALSE" );
            }
        	inventoryPSForm.setAvailableLotsList( availableLotList );

        	request.setAttribute("action", action);

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        return mapping.findForward( "showLots" );
    }

    private boolean showWaferIds(String psTypeName, String psType, ArrayList<LotDetails> availableLotList)
	{
		boolean showWaferIds = false;
		if( availableLotList == null || availableLotList.size() == 0 )
		{
			if ( psTypeName.equalsIgnoreCase( LOCATION_TYPE_FAB ) || psTypeName.equalsIgnoreCase( LOCATION_TYPE_WAFER_STORE )
                    || psTypeName.equalsIgnoreCase( LOCATION_TYPE_SORT ) || "Back Grind".equalsIgnoreCase(psType ) )
            {
			    showWaferIds = true;
            }
            else
            {
                showWaferIds = false;
            }
		}
		else
		{
			for( LotDetails	lotDetail:availableLotList )
			{
				if( lotDetail.getWaferIds() != null && lotDetail.getWaferIds().length() > 0 )
				{
					showWaferIds = true;
					break;
				}
			}
		}
		return showWaferIds;
	}
}
